﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Web.Library.DomainObject.SQLServer;
using System.Collections.ObjectModel;

namespace Web.Library.Facade.SQLServer
{
   public partial class SysMenuFacade
    {
       public virtual List<SysMenu> GetListByParentID(int parentID)
       {
          return dal.GetListByParentID(parentID);
       }

       private string RetrieveTreeChild(int ParentID)
       {
           string str1 = string.Empty;
           try
           {
               //MenuDAL menuDal = new MenuDAL();
               //Collection<Menu> collection1 = new Collection<Menu>();
               //Collection<Menu> collection2 = menuDal.RetrieveMenuByParentID(ParentID);
               List<SysMenu> collection2 = dal.GetListByParentID(ParentID);
               if (collection2.Count > 0)
               {
                   string str2 = "[";
                   List<string> list1 = new List<string>();
                   foreach (SysMenu menu in collection2)
                   {
                       string str3 = string.Empty;
                       List<string> list2 = new List<string>();
                       list2.Add("\"ID\":" + menu.MenuID.ToString());
                       list2.Add("\"Control\": \"<a href='#' class='editGrid' rel='" + menu.MenuID.ToString() + "' title='Edit'></a>&nbsp;<a href='#' class='deleteGrid' rel='" + menu.MenuID.ToString() + "' title='Delete'></a>\"");
                       list2.Add("\"Title\":\"" + menu.Title + "\"");
                       list2.Add("\"Url\":\"" + menu.Url + "\"");
                       string str4 = this.RetrieveTreeChild(menu.MenuID);
                       if (!string.IsNullOrEmpty(str4))
                           list2.Add("\"children\":" + str4);
                       list1.Add("{" + string.Join(",", (IEnumerable<string>)list2) + "}");
                   }
                   str1 = str2 + string.Join(",", (IEnumerable<string>)list1) + "]";
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
           return str1;
       }

       private string RetrieveTreeChild(int GroupID, int ParentID)
       {
           string str1 = string.Empty;
           try
           {
               //MenuDAL menuDal = new MenuDAL();
               //Collection<Menu> collection1 = new Collection<Menu>();
               //Collection<Menu> collection2 = menuDal.RetrieveMenuByParentID(ParentID);

               List<SysMenu> collection2 = dal.GetListByParentID(ParentID);
               if (collection2.Count > 0)
               {
                   string str2 = "[";
                   List<string> list1 = new List<string>();
                   foreach (SysMenu menu in collection2)
                   {
                       string str3 = string.Empty;
                       List<string> list2 = new List<string>();
                       //GroupAuthorization groupAuthorization1 = new GroupAuthorization();
                       //GroupAuthorization groupAuthorization2 = new GroupAuthorizationDAL().RetrieveAuthorizationByGroupIDAndMenuID(GroupID, menu.ID);
                       SysGroupAuthorization groupAuthorization2 = dal.SysGroupAuthorizationByGroupIDAndMenuID(GroupID, menu.MenuID);
                       list2.Add("\"ID\":" + menu.MenuID.ToString());
                       
                       if(groupAuthorization2!=null)
                        list2.Add("\"CheckBox\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='MenuID' " + (string)(groupAuthorization2.MenuID > 0 ? (object)"checked='checked'" : (object)"") + " />\"");
                       else
                           list2.Add("\"CheckBox\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='MenuID' " + " />\"");
                       
                       list2.Add("\"Control\": \"<a href='#' class='editGrid' rel='" + menu.MenuID.ToString() + "' title='Edit'></a>&nbsp;<a href='#' class='deleteGrid' rel='" + menu.MenuID.ToString() + "' title='Delete'></a>\"");
                       list2.Add("\"Title\":\"" + menu.Title + "\"");
                       if (groupAuthorization2 != null)
                       {
                           list2.Add("\"AllowDisplay\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='AllowDisplay' " + (string)(groupAuthorization2.AllowDisplay.ToString().ToLower() == "true" ? "checked='checked'" : (object)"") + " />\"");
                           list2.Add("\"AllowCreate\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='AllowCreate' " + (string)(groupAuthorization2.AllowCreate.ToString().ToLower() == "true" ? "checked='checked'" : (object)"") + " />\"");
                           list2.Add("\"AllowUpdate\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='AllowUpdate' " + (string)(groupAuthorization2.AllowUpdate.ToString().ToLower() == "true" ? "checked='checked'" : (object)"") + " />\"");
                           list2.Add("\"AllowDelete\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='AllowDelete' " + (string)(groupAuthorization2.AllowDelete.ToString().ToLower() == "true" ? "checked='checked'" : (object)"") + " />\"");
                       }
                       else
                       {
                           list2.Add("\"AllowDisplay\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='AllowDisplay' " + " />\"");
                           list2.Add("\"AllowCreate\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='AllowCreate' " + " />\"");
                           list2.Add("\"AllowUpdate\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='AllowUpdate' " + " />\"");
                           list2.Add("\"AllowDelete\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='AllowDelete' " + " />\"");
                       }

                       string str4 = this.RetrieveTreeChild(GroupID, menu.MenuID);
                       if (!string.IsNullOrEmpty(str4))
                           list2.Add("\"children\":" + str4);
                       list1.Add("{" + string.Join(",", (IEnumerable<string>)list2) + "}");
                   }
                   str1 = str2 + string.Join(",", (IEnumerable<string>)list1) + "]";
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
           return str1;
       }

       public string RetrieveMenuTree()
       {
           string str1 = string.Empty;
           try
           {
               //MenuDAL menuDal = new MenuDAL();
               //Collection<Menu> collection1 = new Collection<Menu>();
               List<SysMenu> collection2 = dal.GetListByParentID(0);
               if (collection2.Count > 0)
               {
                   string str2 = "[";
                   List<string> list1 = new List<string>();
                   foreach (SysMenu menu in collection2)
                   {
                       string str3 = string.Empty;
                       List<string> list2 = new List<string>();
                       list2.Add("\"ID\":" + menu.MenuID.ToString());
                       list2.Add("\"Control\": \"<a href='#' class='editGrid' rel='" + menu.MenuID.ToString() + "' title='Edit'></a>&nbsp;<a href='#' class='deleteGrid' rel='" + menu.MenuID.ToString() + "' title='Delete'></a>\"");
                       list2.Add("\"Title\":\"" + menu.Title + "\"");
                       list2.Add("\"Url\":\"" + menu.Url + "\"");
                       string str4 = this.RetrieveTreeChild(menu.MenuID);
                       if (!string.IsNullOrEmpty(str4))
                           list2.Add("\"children\":" + str4);
                       list1.Add("{" + string.Join(",", (IEnumerable<string>)list2) + "}");
                   }
                   str1 = str2 + string.Join(",", (IEnumerable<string>)list1) + "]";
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
           return str1;
       }

       public string ListGroupAuthorizationMenu(int GroupID)
        {
            string str1 = string.Empty;
            try
            {
                //MenuDAL menuDal = new MenuDAL();
                //Collection<Menu> collection1 = new Collection<Menu>();
                //Collection<Menu> collection2 = menuDal.RetrieveMenuByParentID(0);
                List<SysMenu> collection2 = dal.GetListByParentID(0);
                if (collection2.Count > 0)
                {
                    string str2 = "[";
                    List<string> list1 = new List<string>();
                    foreach (SysMenu menu in collection2)
                    {
                        string str3 = string.Empty;
                        List<string> list2 = new List<string>();
                        //GroupAuthorization groupAuthorization1 = new GroupAuthorization();
                        SysGroupAuthorization groupAuthorization1 = new SysGroupAuthorization();
                        //GroupAuthorization groupAuthorization2 = new GroupAuthorizationDAL().RetrieveAuthorizationByGroupIDAndMenuID(GroupID, menu.ID);

                        SysGroupAuthorization groupAuthorization2 = dal.SysGroupAuthorizationByGroupIDAndMenuID(GroupID, menu.MenuID);
                        list2.Add("\"ID\":" + menu.MenuID.ToString());
                        
                        if(groupAuthorization2!=null)
                            list2.Add("\"CheckBox\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='MenuID' " + (string)(groupAuthorization2.AuthID > 0 ? (object)"checked='checked'" : (object)"") + " />\"");
                        else
                            list2.Add("\"CheckBox\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='MenuID' " + " />\"");

                        list2.Add("\"Control\": \"<a href='#' class='editGrid' rel='" + menu.MenuID.ToString() + "' title='Edit'></a>&nbsp;<a href='#' class='deleteGrid' rel='" + menu.MenuID.ToString() + "' title='Delete'></a>\"");
                        list2.Add("\"Title\":\"" + menu.Title + "\"");

                        if (groupAuthorization2 != null)
                        {
                            list2.Add("\"AllowDisplay\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='AllowDisplay' " + (string)(groupAuthorization2.AllowDisplay.ToString().ToLower() == "true" ? "checked='checked'" : (object)"") + " />\"");
                            list2.Add("\"AllowCreate\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='AllowCreate' " + (string)(groupAuthorization2.AllowCreate.ToString().ToLower() == "true" ? "checked='checked'" : (object)"") + " />\"");
                            list2.Add("\"AllowUpdate\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='AllowUpdate' " + (string)(groupAuthorization2.AllowUpdate.ToString().ToLower() == "true" ? "checked='checked'" : (object)"") + " />\"");
                            list2.Add("\"AllowDelete\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='AllowDelete' " + (string)(groupAuthorization2.AllowDelete.ToString().ToLower() == "true" ? "checked='checked'" : (object)"") + " />\"");
                        }
                        else
                        {
                            list2.Add("\"AllowDisplay\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='AllowDisplay' " +  " />\"");
                            list2.Add("\"AllowCreate\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='AllowCreate' " +  " />\"");
                            list2.Add("\"AllowUpdate\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='AllowUpdate' " +  " />\"");
                            list2.Add("\"AllowDelete\": \"<input type='checkbox' value='" + menu.MenuID.ToString() + "' name='AllowDelete' " +  " />\"");
                        }
                        
                        string str4 = this.RetrieveTreeChild(GroupID, menu.MenuID);
                        if (!string.IsNullOrEmpty(str4))
                            list2.Add("\"children\":" + str4);
                        list1.Add("{" + string.Join(",", (IEnumerable<string>)list2) + "}");
                    }
                    str1 = str2 + string.Join(",", (IEnumerable<string>)list1) + "]";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return str1;
        }

       public virtual SysMenu GetByUrl(string Url)
       {
           return dal.GetByUrl(Url);
       }
    }
}
